package com.chanjiedata.changbs.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.chanjiedata.changbs.base.MainApplication;
import com.lcodecore.tkrefreshlayout.utils.DensityUtil;

public class NumberProgressView extends View {

    /**
     * 进度条画笔的宽度（dp）
     */
    private float paintProgressWidth = 10.0f;
    /**
     * 文字百分比的字体大小（sp） 名字
     */
    private int paintTextSize = 13;

    private int position = 0;

    /**
     * 文字百分比的字体大小（sp） 数值
     */
    private int paintNumberTextSize = 12;

    /**
     * 左侧已完成进度条的颜色
     */
    private int paintLeftColor = Color.parseColor("#F5C6DA");

    private int paintLeftColorTwo = Color.parseColor("#8CDDF0");

    /**
     * 右侧未完成进度条的颜色
     */
    private int paintRightColor = Color.parseColor("#E4EFF1");

    /**
     * 百分比文字的颜色
     */
    private int paintTextColor = Color.parseColor("#627885");

    /**
     * 主线程传过来进程 0 - 100
     */
    private int progress = 0;


    private String name = "小姐";

    private String number = "";


    /**
     * 得到自定义视图的宽度
     */
    private int viewWidth = 0;

    /**
     * 得到自定义视图的Y轴中心点
     */
    private int viewCenterY = 0;

    /**
     * 画左边已完成进度条的画笔
     */
    private Paint paintleft = new Paint(Paint.ANTI_ALIAS_FLAG);

    /**
     * 画右边未完成进度条的画笔
     */
    private Paint paintRight = new Paint(Paint.ANTI_ALIAS_FLAG);

    /**
     * 画中间的百分比文字的画笔
     */
    private Paint paintText = new Paint();

    /**
     * 要画的文字的宽度
     */
    private int textWidth = 0;

    /**
     * 画文字时底部的坐标
     */
    private float textBottomY = 0f;

    /**
     * 要画的名字文字的宽度
     */
    private int textNameWidth = 0;


    /**
     * 画文字时底部的坐标
     */
    private float textNameBottomY = 0f;


    /**
     * 包裹文字的矩形
     */
    private Rect rect = new Rect();

    /**
     * 包裹左边名字的矩形
     */
    private Rect rectName = new Rect();

    /**
     * 文字总共移动的长度（即从0%到100%文字左侧移动的长度）
     */
    private int totalMovedLength = 0;

    public NumberProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        // 构造器中初始化数据
        initData(context);
    }

    private void initData(Context context) {
        //设置进度条画笔的宽度
        int paintProgressWidthPx = DensityUtil.dp2px(context, paintProgressWidth);

        //设置百分比文字的尺寸
        int paintTextSizePx = sp2px(context, paintTextSize);

        // 已完成进度条画笔的属性
        paintleft.setColor(paintLeftColor);
        paintleft.setStrokeWidth(paintProgressWidthPx);

        paintleft.setStrokeCap(Paint.Cap.ROUND);//使圆弧两头圆滑
        paintleft.setStyle(Paint.Style.STROKE);

        // 未完成进度条画笔的属性

        paintRight.setColor(paintRightColor);
        paintRight.setStrokeWidth(paintProgressWidthPx);
        paintRight.setStrokeCap(Paint.Cap.ROUND);//使圆弧两头圆滑
        paintRight.setStyle(Paint.Style.STROKE);


        // 百分比文字画笔的属性
        paintText.setColor(paintTextColor);
        paintText.setTextSize(paintTextSizePx);
        paintText.setAntiAlias(true);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        getWidthAndHeight();
    }

    private void getWidthAndHeight() {

        //得到自定义视图的高度
        int viewHeight = getMeasuredHeight();
        viewWidth = getMeasuredWidth();
        viewCenterY = viewHeight / 2;

        textBottomY = viewCenterY - DensityUtil.dp2px(getContext(), 10f);
        textNameBottomY = textBottomY;
        totalMovedLength = (viewWidth - DensityUtil.dp2px(getContext(), 30f));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //得到float型进度
        float progressFloat = progress / 100.0f;

        //当前文字移动的长度
        float currentMovedLentgh = totalMovedLength * progressFloat;



        canvas.drawText(name, DensityUtil.dp2px(getContext(), 15f), textNameBottomY, paintText);



        //画右侧未完成的进度条，这个进度条的长度不是严格按照百分比来缩放的，因为文字的长度会变化，所以它的长度缩放比例也会变化
        canvas.drawLine(
                DensityUtil.dp2px(getContext(), 15f),
                viewCenterY,
                totalMovedLength+DensityUtil.dp2px(getContext(), 15f),
                viewCenterY,
                paintRight
        );

        //画左侧已经完成的进度条，长度为从Veiw左端到文字的左侧
        if (position % 2 == 1) {
            paintleft.setColor(paintLeftColorTwo);
        }
        canvas.drawLine(
                DensityUtil.dp2px(getContext(), 15f),
                viewCenterY,
                currentMovedLentgh+DensityUtil.dp2px(getContext(), 15f),
                viewCenterY,
                paintleft
        );
        //画文字(注意：文字要最后画，因为文字和进度条可能会有重合部分，所以要最后画文字，用文字盖住重合的部分)
        //得到包围文字的矩形的宽高
        paintText.getTextBounds(name, 0, name.length(), rectName);
        textNameWidth = rectName.width();
        canvas.drawText(number,   textNameWidth  + DensityUtil.dp2px(getContext(), 30)  , textBottomY, paintText);
    }

    /**
     * @param progress 外部传进来的当前进度
     */
    public void setData(int progress , String name, String number, int position) {
        this.progress = progress;
        this.name = name;
        this.number = number;
        this.position = position;
        invalidate();
    }


    /**
     * 根据手机的分辨率从 sp 的单位 转成为 px
     */
    public static int sp2px(Context context, float spValue) {
        float fontScale = MainApplication.getAppContext().getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }
}
